<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function checkForm(){
		var costTypeIds = document.getElementsByName("costTypeId");
		var costValues = document.getElementsByName("costValue");
		var customerNames = document.getElementsByName("customerName");
		var costDescs = document.getElementsByName("costDesc");
		
		var sign = 0;
		for(var i = 0; i < costValues.length; i++){
			if(costValues[i].value != "" 
				|| costTypeIds[i].value != 0 
				|| customerNames[i].value != "" 
				|| costDescs[i].value != ""){
				if(costTypeIds[i].value == 0){
					alert("请选择费用类型！");
					return false;
				}
				if(costValues[i].value == ""){
					alert("费用金额<fmt:message key='common.message.empty'/>");
					return false;
				}
				/**
				if(customerNames[i].value == ""){
					alert("请选择客户名称!");
					return false;
				}*/
			}
			
			if(costValues[i].value != "" 
				&& costTypeIds[i].value != 0){ 
				//&& customerNames[i].value != ""){
				sign = sign + 1;
			}
		}
		if(sign <= 0){
			alert("费用记录不能为空，请追加费用记录！");
			return false;
		}
		return true;
	}

	function back(){
		var uri = "./saleCost.do?action=waitAuditSaleCost";
		var sign = "<c:out value='${sign}'/>";
		if(sign == "1"){//返回到待审的销售费用页面
			uri = "./saleCost.do?action=waitAuditSaleCost";
		}else if(sign == "2"){//返回到退回的销售费用页面
			uri = "./saleCost.do?action=returnSaleCost";
		}
		window.location.href = uri;
		window.location.target = "main";
	}

	function customerDialog(rowNum){
		var url = "./contract.do?action=selectMyAllCustomer";
		var dlgParam = "dialogHeight:360px;dialogWidth:430px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取客户id和客户名称
			var customerId = "";
			var customerName = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				customerId = tStr[0];
				customerName = tStr[1];
			}
			document.getElementsByName("customerId")[Number(rowNum) - 1].value = customerId;										
			document.getElementsByName("customerName")[Number(rowNum) - 1].value = customerName;
		}
	}
	
	function getCurrentTotalCost(obj){
		onlyNumFloat(obj);
		var costValues = document.getElementsByName("costValue");
		var currentTotalCost = 0;
		for(var i = 0; i < costValues.length; i++){
			currentTotalCost += Number(costValues[i].value);
		}
		document.getElementById("currentTotalCost").innerText = currentTotalCost;
	}
	
	function save(){
		if(checkForm()){
			document.updateSaleCostForm.action = "./saleCost.do?action=updateSaleCost";
			document.updateSaleCostForm.target = "main";
			document.updateSaleCostForm.submit();
		}
	}
	
	//追加行start
	function addRow(tableId){//增加tab的一行	
		try{				
			var objT =  document.getElementById(tableId);//通过ID 得到对象
			if(objT==null){	alert("没有可操作的表！");return;	}
			if(objT.rows.length<1){ alert("没有表格标题行"); return;}
			
			var newTr = objT.insertRow(-1);//插入一行
			newTr.className = "table_style";	//为新行赋样式
			var colsLength = objT.rows(0).cells.length;  //表中列总数 
			var newTd ;
			var hiddenObj;		
			var insertTdHtml = prepareInsertHtml(newTrNum);//获得新增行各个TD的HTML代码
			var i=0;
			for(i=0;i<colsLength;i++){
				newTd = newTr.insertCell();
				newTd.innerHTML = insertTdHtml[i];
				newTd.align="left";				
			}
			newTrNum++;//表格总行数加1
		}catch(Exception){}
	}
	
	//增加行所需代码
	var newTrNum = 0;//表格最大行号，只增不减
	var insertHtml = new Array();//用于插入新记录的HTML
	function prepareInsertHtml(num){
		insertHtml[0] = "<select id='costTypeId' name='costTypeId' class='input_percent'>";
		insertHtml[0] += "<option value='0'>------请选择------</option>";
		<c:forEach var="range" items="${costTypes}">
			insertHtml[0] += "<option value='${range.id}'>${range.rangeValue}</option>";
		</c:forEach>
		insertHtml[0] += "</select>";
		
		insertHtml[1] = "<input id='costValue' name='costValue' class='input_percent' type='text' onkeyup='getCurrentTotalCost(this)' onblur='getCurrentTotalCost(this)'/>";
			
		insertHtml[2] = "&nbsp;<input id='customerName' name='customerName' class='input_percent2' type='text' readonly='readonly'/>&nbsp;&nbsp;";
		insertHtml[2] += "<input class='btn_select' type='button' value='' onclick='customerDialog("+num+")'/>";
		insertHtml[2] += "<input id='customerId' name='customerId' type='hidden'/>";
			
		insertHtml[3] = "<input id='costDesc' name='costDesc' class='input_percent' type='text'/>";	
		return insertHtml;
	}
	//追加行end
	
	function initPage(){
		newTrNum = table1.rows.length;//表格最大行号，只增不减
		
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.updateSuccess'/>");
		}
	}
-->
</script>
</head>
<body onload="initPage()">
<form name="updateSaleCostForm" action="" method="post">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">修改费用</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<button class="btn_submit" onclick="save()" hidefocus="true">提交</button>&nbsp;&nbsp;
							<button class="btn_back" onclick="back()" hidefocus="true">取消并返回</button>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title">单据编号：</td>
									<td>
										${saleCostCode}
										<input id="saleCostCode" name="saleCostCode" type="hidden" value="${saleCostCode}"/>
									</td>
									<td class="title">当前累计费用：</td>
									<td><span id="currentTotalCost">${totalCost}</span>元</td>
								</tr>
								<c:forEach var="saleCost" items="${saleCosts}" begin="0" end="0">
								<tr>
									<td class="title">申请人：</td>
									<td>
											${saleCost.applyPerson}
											<input id="applyPersonId" name="applyPersonId" type="hidden" value="${saleCost.applyPersonId}"/>
									</td>
									<td class="title">申请日期：</td>
									<td>
											<fmt:formatDate value="${saleCost.applyDate}" pattern="yyyy-MM-dd"/>
											<input id="applyDate" name="applyDate" type="hidden" 
											value="<fmt:formatDate value='${saleCost.applyDate}' pattern='yyyy-MM-dd'/>" />
									</td>
								</tr>
								</c:forEach>
							</table>
							<table class="table_add">
								<tr>
									<td class="title_top"><button class="btn_append" onclick="addRow('table1')" hidefocus="true">追加行</button></td>
								</tr>
							</table>
							<table id="table1" class="table_style">
								<tr>
									<td class="title" width="20%">费用类别</td>
									<td class="title" width="12%">费用金额(元)</td>
									<td class="title" width="38%">客户名称</td>
									<td class="title" width="30%">用途描述</td>
								</tr>
								<c:forEach var="saleCost" items="${saleCosts}" varStatus="s">
								<tr>
									<td>
										<select id="costTypeId" name="costTypeId" class="input_percent">
											<option value="0" <c:if test='${saleCost.costTypeId == 0}'>selected</c:if> >------请选择------</option>
											<c:forEach var="range" items="${costTypes}">
											<option value="${range.id}" <c:if test='${saleCost.costTypeId == range.id}'>selected</c:if> >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<input id="costValue" name="costValue" class="input_percent" type="text" onkeyup="getCurrentTotalCost(this)" 
											onblur="getCurrentTotalCost(this)" value="${saleCost.costValue}"/>
									</td>
									<td>
										<input id="customerName" name="customerName" class="input_percent2" type="text" readonly="readonly" value="${saleCost.customerName}"/>&nbsp;
										<input class="btn_select" type="button" value="" onclick="customerDialog(${s.count})"/>
										<input id="customerId" name="customerId" type="hidden" value="${saleCost.customerId}"/>
									</td>
									<td><input id="costDesc" name="costDesc" class="input_percent" type="text" value="${saleCost.costDesc}"/></td>
								</tr>
								</c:forEach>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<input id="sign" name="sign" type="hidden" value="${sign}"/>
</form>	
</body>
</html>